package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.db.dao.YhjjdxDao;
import com.sandking.db.bean.Yhjjdx;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhjjdxJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhjjdx getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhjjdx yhjjdx = null;
		try{
			String key = "Yhjjdx_Object";
			String field = SK_Plus.b("id:",id).e();
			yhjjdx = Yhjjdx.createForJson(jedis.hget(key,field));
			if(yhjjdx == null){
				yhjjdx = YhjjdxDao.getById(id);
				if(yhjjdx!=null){
					loadCache(yhjjdx);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhjjdx;
	}
	
	/**
	 * 根据( 用户id ) 查询
	 */
	public static List<Yhjjdx> getByYhid(int yhid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhjjdx> yhjjdxs = new ArrayList<Yhjjdx>();
		try{
			String key = "Yhjjdx_Index";
			String field = SK_Plus.b(key,"yhid:",yhid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhjjdx_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhjjdxs = Yhjjdx.createForJson(jsons);
			}else{
				yhjjdxs = YhjjdxDao.getByYhid(yhid);
				if(yhjjdxs!=null && !yhjjdxs.isEmpty()){
					loadCaches(yhjjdxs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhjjdxs;
	}
	
	/**
	 * 根据( 目标用户id ) 查询
	 */
	public static List<Yhjjdx> getByMbyhid(int mbyhid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhjjdx> yhjjdxs = new ArrayList<Yhjjdx>();
		try{
			String key = "Yhjjdx_Index";
			String field = SK_Plus.b(key,"mbyhid:",mbyhid).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhjjdx_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhjjdxs = Yhjjdx.createForJson(jsons);
			}else{
				yhjjdxs = YhjjdxDao.getByMbyhid(mbyhid);
				if(yhjjdxs!=null && !yhjjdxs.isEmpty()){
					loadCaches(yhjjdxs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhjjdxs;
	}
	
	/**
	 * 根据( 用户id  有效 ) 查询
	 */
	public static List<Yhjjdx> getByYhidYx(int yhid, boolean yx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhjjdx> yhjjdxs = new ArrayList<Yhjjdx>();
		try{
			String key = "Yhjjdx_Index";
			String field = SK_Plus.b(key,"yhid, yx:",yhid, yx).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhjjdx_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhjjdxs = Yhjjdx.createForJson(jsons);
			}else{
				yhjjdxs = YhjjdxDao.getByYhidYx(yhid, yx);
				if(yhjjdxs!=null && !yhjjdxs.isEmpty()){
					loadCaches(yhjjdxs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhjjdxs;
	}
	

	public static void loadCache(Yhjjdx yhjjdx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhjjdx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhjjdx yhjjdx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhjjdx_Object";
		String field = SK_Plus.b("id:",yhjjdx.getId()).e();
		String data = yhjjdx.toJson();
		p.hset(key,field,data);
		
		String primaryKey = String.valueOf(yhjjdx.getId());
		key = "Yhjjdx_Index";
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yhid:",yhjjdx.getYhid()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"mbyhid:",yhjjdx.getMbyhid()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"yhid, yx:",yhjjdx.getYhid(),yhjjdx.getYx()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhjjdx> yhjjdxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhjjdx yhjjdx : yhjjdxs){
				loadCache(yhjjdx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhjjdx yhjjdx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhjjdx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhjjdx yhjjdx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhjjdx_Object";
		String field = SK_Plus.b("id:",yhjjdx.getId()).e();
		p.hdel(key,field);
		
		key = "Yhjjdx_Index";
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(yhjjdx.getId());
		field = SK_Plus.b(key,"yhid:",yhjjdx.getYhid()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"mbyhid:",yhjjdx.getMbyhid()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"yhid, yx:",yhjjdx.getYhid(),yhjjdx.getYx()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhjjdx> yhjjdxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhjjdx yhjjdx : yhjjdxs){
				clearCache(yhjjdx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhjjdx insert(Yhjjdx yhjjdx){
		yhjjdx = YhjjdxDao.insert(yhjjdx);
		if(yhjjdx!=null){
			loadCache(yhjjdx);
		}
    	return yhjjdx;
    }
    
    public static Yhjjdx update(Yhjjdx yhjjdx){
    	yhjjdx = YhjjdxDao.update(yhjjdx);
    	if(yhjjdx!=null){
			loadCache(yhjjdx);
		}
    	return yhjjdx;
    }
    
    public static boolean delete(Yhjjdx yhjjdx){
    	boolean bool = YhjjdxDao.delete(yhjjdx);
    	if(bool){
    		clearCache(yhjjdx);
    	}
    	return bool;
    }
}